package stuff.hib;

import stuff.Account;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import stuff.AccountManager;

/**
 *
 * @author Николай
 */
@Repository
@Transactional
public class HibAccountManager implements AccountManager {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    @Transactional
    public void addAccount(Account acc) {
        sessionFactory.getCurrentSession().save(acc);
    }

    @Override
    @Transactional(readOnly = true)
    public Account getAccount(String userName) {
        return (Account) sessionFactory.getCurrentSession().
                createCriteria(Account.class).
                add(Restrictions.eq("userName", userName)).
                uniqueResult();
    }

    @Override
    @Transactional(readOnly = true)
    public Account getAccount(long id) {
        return (Account) sessionFactory.getCurrentSession().
                load(Account.class, id);
    }
}
